//
//  MusicAVPlayer.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/4.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "MusicAVPlayer.h"
#import "PlayViewController.h"
@implementation MusicAVPlayer

+(instancetype)shareMusicAVPlayer {
    static MusicAVPlayer *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        player = [[MusicAVPlayer alloc]init];
        
        player.videoProgress = [[UIProgressView alloc]init];
        
    });
    return player;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.style = 0;
        self.isPlay = YES;
        
    }
    return self;
}

#pragma mark - ------------ 播放 ---------------
- (void)playWithURL:(SongListSongsModel *)songListModel {
    
    if (_songsModel != songListModel) {
        [_songsModel release];
        _songsModel = [songListModel retain];
    }
    if (self.currentItem) {
        [self replaceCurrentItemWithPlayerItem:self.currentItem];
    }
    
    NSLog(@"%ld", _songsModel.songs.count);
    self.songDetailModel = songListModel.songs[self.local];
    
    if (_songDetailModel.urlList.count != 0) {
        
        SongListUrlListModel *urlListModel = _songDetailModel.urlList[0];
        self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlListModel.url]];
        [self replaceCurrentItemWithPlayerItem:self.playerItem];
        
    }else if (_songDetailModel.auditionList.count != 0) {
        
        SongListAuditionListModel *auditionListModel = _songDetailModel.auditionList[0];
        self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:auditionListModel.url]];
        [self replaceCurrentItemWithPlayerItem:self.playerItem];
        
    }else {
        
        
    }
    
    [PlayViewController SharePlayViewController].songDetailModel = songListModel;
    RankListSongListSongsModel *songListSongsModel = songListModel.songs[_local];
     [PlayViewController SharePlayViewController].commandModel = songListSongsModel;
    
    /**< 播放的NSTimer */
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    [self play];
}
#pragma mark - 自定义的播放方法，内部用，外部不用
- (void)customPlay {
    if (self.currentItem) {
        
        [self replaceCurrentItemWithPlayerItem:self.currentItem];
    }
//    SongListUrlListModel *urlModel = _songDetailModel.urlList[0];
//    AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:urlModel.url]];
//    [self replaceCurrentItemWithPlayerItem:playerItem];
//    [self play];
    
    if (_songDetailModel.urlList.count != 0) {
        
        SongListUrlListModel *urlListModel = _songDetailModel.urlList[0];
        self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlListModel.url]];
        [self replaceCurrentItemWithPlayerItem:self.playerItem];
        
    }else if (_songDetailModel.auditionList.count != 0) {
        
        SongListAuditionListModel *auditionListModel = _songDetailModel.auditionList[0];
        self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:auditionListModel.url]];
        [self replaceCurrentItemWithPlayerItem:self.playerItem];
        
    }else {
        
        
    }
    
    [self play];

}
#pragma mark - ------------ 播放模式 ---------------

- (void)timerAction:(NSTimer *)sender {
    
    switch (_style) {
            /**< 顺序 */
        case 0:{
            
            if (CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration) == 1) {
                
                _local += 1;
                self.songDetailModel = self.songsModel.songs[self.local];
                NSLog(@"🆚%@", self.songDetailModel);
//                [self customPlay];
                [self playWithURL:self.songsModel];
            }

            
            break;
        }
          /**< 单曲 */
        case 1:{
            
            if (CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration) == 1) {
                
                _songDetailModel = _songsModel.songs[_local];
                
//                [self customPlay];
                [self playWithURL:self.songsModel];
            }

            
            break;
        }
        /**< 随机 */
        default:{
            
            if (CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration) == 1) {
                
                _songDetailModel = _songsModel.songs[arc4random()%_songsModel.songs.count];
//                [self customPlay];
                [self playWithURL:self.songsModel];

            }

            break;
        }
    }
    
}

@end
